<?php
namespace app\index\controller;

use \app\backend\model\Menu;
use app\backend\org\HeadTitle;
use \think\Db;
use app\backend\org\Crumbs;
use app\backend\org\Kv;

/**
 * 学术活动控制器
 */
class Academical  extends \think\Controller
{
    //领星学术接口
    public function index(){
        try{
            $menu_id = input('menu_id','-1','htmlspecialchars');//第三个参数过滤
            $language = input("lang","cn","htmlspecialchars");
            //当前页数
            //$page=input('page','1','htmlspecialchars');
            //每页显示条数
            $psize=input('psize','1','htmlspecialchars');
            //当前所在tab 的页数
            $tp1=input('tp1','1','htmlspecialchars');
            $tp2=input('tp2','1','htmlspecialchars');
            $tp3=input('tp3','1','htmlspecialchars');
            //当前所在tab
            $ct=input('ct','1','htmlspecialchars');
            //计算偏移量
            $offset =array(($tp1 - 1)*$psize,($tp2 - 1)*$psize,($tp3 - 1)*$psize);

            if($language=="cn"){
                $tab_name =  "tab_name_cn";
                $content = "content_cn";
                $list_title = "list_title_cn";
                $title_introduce = "title_introduce_cn";
            }elseif($language=="en"){
                $tab_name =  "tab_name_en";
                $content = "content_en";
                $list_title = "list_title_en";
                $title_introduce = "title_introduce_en";
            }
            $data = Menu::All(["p_id"=>["exp","<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data,$menu_id,$language,"menu");
            //echo "<pre>";
           // print_r($head_title);exit;
            //调用kv类查kv画面内容
            $menu=Kv::Kv($menu_id);
            //echo "<pre>";
            //print_r($menu);exit;
            //领星学术的独立介绍
           $menu_content =  Db::query("select $content as content,type_id from ls_other where `p_id`=".$menu[0]['menu_id']." and `delete_time` is null ");

            //tab分类切换
          // $tab = Db::query("select tab_id,$tab_name,type_id from ls_tab where `p_id`={$menu[0]['menu_id']} and `delete_time` is null order by sort ASC ");
            $tab = Db::query("select b.tab_id,b.$tab_name as tab_name,b.type_id,t.tt_id from ls_tab b left join ls_tab_type t on b.tab_id=t.tab_id where b.p_id={$menu[0]['menu_id']} and b.delete_time is null order by b.sort ASC ");

            //循环拼接list表内容
           /* foreach ($tab as $k=>$v) {
                //计算总页数
                $count = Db::query("select count(*) as total from ls_list where `p_id`={$v['tab_id']} and `is_recommend`!='1,{$v['tt_id']}'  and `delete_time` is null and `is_enable`=1 or (`news_id` LIKE '%{$v['tab_id']}%')");
               // var_dump($count);exit;
                foreach ($count as $a=>$b){
                    $page_count = ceil($b['total'] / $psize);
                }
            }*/


            //循环拼接list表内容
            foreach ($tab as $k=>$v){
                //计算每个tab的分页
                $count1 = Db::query("select count(*) as total from ls_list where `p_id`={$v['tab_id']} and `is_recommend`!='1,{$v['tt_id']}' and `delete_time` is null and `is_enable`=1 or (`news_id` LIKE '%{$v['tab_id']}%')" );
                // var_dump($count1);exit;
                foreach ($count1 as $a=>$b){
                    $page_count1 = ceil($b['total'] / $psize);
                }
                $tab[$k]["page_count"] = $page_count1;
                //var_dump($page_count1);

                //当前tab
                if($ct == ($k+1)){
                    $tab[$k]["ct"] = 1;
                }

                //置顶的新闻
                $list1 = Db::query("select list_id,p_id,news_id,type_id,is_recommend,$list_title as list_title,$title_introduce as title_introduce,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where p_id={$v['tab_id']} and `is_recommend`='1,{$v['tt_id']}' and `delete_time` is null and `is_enable`=1  ");
                //tab分类下面list表的内容
                //$list2 = Db::query("select list_id,p_id,type_id,is_recommend,$list_title as list_title,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where p_id={$v['tab_id']} and `is_recommend`='0,{$v['tt_id']}' and `delete_time` is null and `is_enable`=1 order by manual_sort ASC limit $offset,$psize ");

                //$list = array_merge($list1,$list2);

                //查询出p_id= news_id包含32 的数据
                //foreach ($offset as $i=>$j){
                    $list_other = Db::query("select list_id,news_id,p_id,type_id,is_recommend,$list_title as list_title,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_list where ((`p_id`={$v['tab_id']} and `is_recommend`='0,{$v['tt_id']}') or (news_id LIKE '%{$v['tab_id']}%')) and `delete_time` is null and `is_enable`=1 order by create_time DESC limit {$offset[$k]},{$psize} ");
               // }
                //var_dump($offset);exit;

                $list = array_merge($list1, $list_other);
                //var_dump($list);
               //exit;
                if(!empty($list)){
                    foreach ($list as $a=>$b){
                      $news_id_array =explode(',',$b["news_id"]);
                        if($v["tab_id"]==$b["p_id"] || in_array($v["tab_id"],$news_id_array)){
                            if (isset($tab[$k]["child"])) {
                                $tab[$k]["child"][] = $b;
                            } else {
                                $tab[$k]["child"] = [$b];
                            }
                        }
                    }
                }else{
                    if (isset($tab[$k]["child"])) {
                        $tab[$k]["child"][] = [];
                    } else {
                        $tab[$k]["child"] = [""];
                    }
                }
            }
            /*var_dump($list);
            exit;*/

            //exit;
            //获取面包屑
           $crumbs =  Crumbs::crumbs($data,$menu_id,$language,"menu");
           // echo "<pre>";
            //print_r($crumbs);exit;
            //判断记录存不存在
            if( empty($menu)){
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            }else{
                return json([
                    'code' => 200,
                    'head_title'=>$head_title,
                    'KV'=>$menu[0],
                    'crumbs'=>$crumbs,
                    'menu_content'=>$menu_content[0],
                    'tab'=>$tab,
                    //'page_count'=>$page_count,
                    "imsg" => "领星学术-academical.html"
                ]);
            }
        }catch (\Exception $e){
//			$this->_log2db('index::被 catch 了');
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败 '.$e->getMessage(),
            ]);
        }
    }

    //领星学术新闻详情接口
    public function main(){
        try{
            $list_id = input('list_id','-1','htmlspecialchars');//第三个参数过滤
            $language = input("lang","cn","htmlspecialchars");
            if($language=="cn"){
                $stage_title = "stage_title_cn";
                $content = "content_cn";
            }elseif($language=="en"){
                $stage_title = "stage_title_en";
                $content = "content_en";
            }

            $tab_id =  Db::query("select p_id from ls_list where `list_id`={$list_id} and `delete_time` is null");
            $menu_id = Db::query("select p_id from ls_tab where `tab_id`={$tab_id[0]["p_id"]} and `delete_time` is null ");
            //print_r($menu_id);exit;
            //调用kv类查kv画面内容
            $menu=Kv::Kv($menu_id[0]["p_id"]);

            //获取stage段落
            $stage = Db::query("select stage_id,type_id,$stage_title as stage_title,$content as content,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_stage where `p_id`={$list_id} and `delete_time` is null and `is_enable`=1 order by `manual_sort` ASC ");
            if(empty($stage)){
                $stage[0]="";
            }
             //echo "<pre>";
            //print_r($stage);exit;
            $data = Menu::All(["p_id"=>["exp","<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data,$list_id,$language,"list");

            //获取面包屑
            $crumbs =  Crumbs::crumbs($data,$list_id,$language,"list");
            //exit;
            // echo "<pre>";
           // print_r($crumbs);exit;
           // return json($crumbs);exit;
            //判断记录存不存在
            if( empty($menu)){
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            }else{
                return json([
                    'code' => 200,
                    'head_title'=>$head_title,
                    'KV'=>$menu[0],
                    'crumbs'=>$crumbs,
                    'stage'=>$stage[0],
                    "imsg" => "领星学术-academical-news-1.html"
                ]);
            }
        }catch (\Exception $e){
//			$this->_log2db('index::被 catch 了');
            print_r($e);
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败'.$e->getMessage(),
            ]);
        }
}

}
?>